package com.example.pbj;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PBJSQLiteHelper extends SQLiteOpenHelper{
	

	public static final String TABLE_MESSAGES = "messages";
	public static final String TABLE_USERS = "users";
	public static final String COLUMN_ID = "mid"; 
	public static final String COLUMN_USERNAME = "musername";
	public static final String COLUMN_SENDER = "sender";
	public static final String COLUMN_MESSAGE = "message";
	public static final String COLUMN_TIME = "time";
	public static final String COLUMN_USER_ID = "uid";
	public static final String COLUMN_USER = "username";
	public static final String COLUMN_PASSWORD = "password";
	
	private static final String DATABASE_NAME = "messenger.db";
	private static final int DATABASE_VERSION = 1;
	
	private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_MESSAGES + "(" + 
			COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
			COLUMN_USERNAME + " VARCHAR, " +
			COLUMN_SENDER + " VARCHAR, " +
			COLUMN_MESSAGE + " VARCHAR, " + 
			COLUMN_TIME + " VARCHAR)";
	
	private static final String DATABASE_USERS_CREATE = "CREATE TABLE " + TABLE_USERS + "(" + 
			COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
			COLUMN_USER + " VARCHAR, " +
			COLUMN_PASSWORD + " VARCHAR)";
	
	private static final String INSERT_DEFAULT_USER = "INSERT INTO users (uid,username,password) VALUES (1,'user','pass')"; 
	
	public PBJSQLiteHelper(Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
	 public void onCreate(SQLiteDatabase database) {
	    database.execSQL(DATABASE_CREATE);
	    database.execSQL(DATABASE_USERS_CREATE);
	    database.execSQL(INSERT_DEFAULT_USER);
	  }

	@Override
	  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    Log.w(PBJSQLiteHelper.class.getName(),
	        "Upgrading database from version " + oldVersion + " to "
	            + newVersion + ", which will destroy all old data");
	    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGES);
	    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
	    onCreate(db);
	  }


}
